package com.jbookshelf.beans;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.jbookshelf.dal.providers.UsersDataProvider;

@Component
@Scope("request")
public class UsersValidation {
	

	@Autowired
	UsersDataProvider dataProvider;


	public void validateLogin(FacesContext arg0, UIComponent arg1, Object arg2)
			throws ValidatorException {
		if (dataProvider.getUserByLogin((String) arg2)!=null){
			FacesContext.getCurrentInstance().addMessage(null, 
					 new FacesMessage(FacesMessage.SEVERITY_WARN,"This username already exists", 
							 "Please enter a different username"));
		}
		
		
	}

	public void validateEmail(FacesContext arg0, UIComponent arg1, Object arg2)
			throws ValidatorException {
		if (dataProvider.getUserByEmail((String) arg2)!=null){
			 FacesContext.getCurrentInstance().addMessage(null, 
					 new FacesMessage(FacesMessage.SEVERITY_WARN,"User with this email already exists", 
							 "Please enter a different email"));
		}
		
		
	}


}
